package com.ruoyi.bigdata.service;

import com.ruoyi.bigdata.domain.vo.ZipInfo;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class DataWorkService {
    static final String WORK_FLOW_NAME = "workflow.xml";

    public static List<ZipInfo> parseZipFile(MultipartFile file) throws Exception {
        List<ZipInfo> fileList = new ArrayList<>();
        try (InputStream is = file.getInputStream(); ZipInputStream zis = new ZipInputStream(is, Charset.forName("GBK"))) {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (WORK_FLOW_NAME.equals(entry.getName())) {
                    // 解析xml文件
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    // 解析XML文件
                    Document doc = builder.parse(zis);
                    doc.getDocumentElement().normalize();
                    NodeList taskList = doc.getElementsByTagName("Node");
                    for (int i = 0; i < taskList.getLength(); i++) {
                        taskList.item(i).getAttributes().getNamedItem("name").getNodeValue();
                    }
                }
            }
        }
        return fileList;
    }

}

